<template>
  <div
    class="chart-support"
    :style="{height: height === 'auto' ? '200px' : height + 'px'}"
  >
    <div class="titleContent">
      <div class="name">{{defaultConfig.numberName}}</div>
      <div class="content">
        <div class="number">{{data.number}}</div>
        <div class="rate"><span class="span1">{{defaultConfig.rateName}}</span> <span class="span2">{{data.rate}}</span></div>
      </div>
    </div>
    <div
      class="echart"
      ref="myChart"
    ></div>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  data() {
    return {
      data: {
        rate: 9800,
        number: 18802,
        xData: ["输电1", "变电2", "配电", "电缆","电缆1","电缆2"], // x轴
        yData: [10, 52, 20, 34,14,15], // y轴
      }, // 得到的%
      defaultConfig: {
        rateName: "输电",
        numberName: "保障人员",
      },
      height: 200,
    };
  },
  watch:{
    data:function(){
      this.drawChart();
    }
  },
  mounted() {
    this.drawChart();
    setTimeout(()=>{
      this.start();
    },1000);
  },
  methods: {
    drawChart() {
      const myChart =echarts.init(this.$refs.myChart);
      const option = {
        // 标题
        title: {
          show: this.showName,
          text: this.title,
        },
        color: ["#419C81"],
        tooltip: {
          trigger: "axis",
        },
        grid: {
          show: true,
          left: "-4%",
          right: "0",
          top: "4%",
          bottom: "4%",
          containLabel: true,
          // backgroundColor: "rgba(211,218,224,0.2)",
          borderColor: "transparent",
          // backgroundColor:new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
          //           offset: 0.8,
          //           color: 'rgba(255, 255, 255, 0.18)'
          //       }, {
          //           offset: 0.18,
          //           color: 'rgba(255, 255, 255, 0.18)'
          //       }])
        },
        xAxis: [
          {
            type: "category",
            data: this.data.xData,
            axisTick: {
              show: false,
              alignWithLabel: true,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#fff",
                fontSize: 10,
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              formatter: function () {
                return "";
              },
            },
          },
        ],
        series: [
          {
            type: "bar",
            barWidth: "14%",
            data: this.data.yData,
            itemStyle: {
              barBorderRadius: 5,
            },
          },
        ],
      };
      myChart.setOption(option);
    },
    start(){
      alert(123);
      this.data={
        rate: 98000,
        number: 1802,
        xData: ["输电1", "变电2", "配电", "电缆","电缆1","电缆2"], // x轴
        yData: [10, 52, 20, 34,140,150], // y轴
      }
    }
  
  },
};
</script>

<style lang="css" scoped>
.chart-support {
  border-bottom: 2px solid rgb(255,255,255,0.3);
  color: #fff;
  width:260px;
}
.chart-support .titleContent {
  height: 52px;
  border-left: 2px solid #006f4eff;
}
.chart-support .name {
  height: 40%;
  font-weight: 500;
  display: flex;
  font-size: 14px;
  align-items: center;
  padding-left: 7px;
}
.chart-support .content {
  height: 60%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 7px;
}
.chart-support .number {
  font-size: 28px;
  font-family: DINPro;
  font-weight: 400;
}
.chart-support .rate {
  width: 86px;
  height: 100%;
  font-size: 14px;
  background: #419c81;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  border-radius: 4px;
}
.chart-support .rate .span2 {
  margin-left: 4px;
  font-size: 22px;
}
.chart-support .rate .span1 {
  display: flex;
  justify-content: center;
  align-items: center;
}
.chart-support .echart {
  width: 100%;
  height: calc(100% - 52px);
}
</style>